import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:hkzf_flutter/utils/common_toast.dart';
import 'package:hkzf_flutter/utils/dio_http.dart';
import 'package:hkzf_flutter/utils/public.dart';

// 注册页面
class RegisterPage extends StatefulWidget {
  const RegisterPage({super.key});
  @override
  State<RegisterPage> createState() => _RegisterPageState();
}

class _RegisterPageState extends State<RegisterPage> {
   var userNameController = TextEditingController();
   var passwordController = TextEditingController();
   var repeatPasswordController = TextEditingController();
  // 注册出来逻辑
  _registerHandler() async{
    var userName = userNameController.text;
    var password = passwordController.text;
    var repeatPassword = repeatPasswordController.text;
    if(isNullOrEmpty(userName) || isNullOrEmpty(password)){
      CommonToast.showToast('请填写用户名或密码不能为空');
      return;
    }
    if(password != repeatPassword){
      CommonToast.showToast('两次输入的密码不一致');
      return;
    }
    var params = {
      "username": userName,
      "password": password
    };
    // 用户注册
    var path = '/user/registered';
    // 提交注册
    var res=await DioHttp.of(context).post(path,params);
    var resMap = json.decode(res.toString());
    int status = resMap['status'];
    String desc = resMap['description'] ?? '内部错误';
    print('结果：${resMap}');
    // 展示接口提示信息
    CommonToast.showToast(desc);
    if(status.toString().startsWith('2')){
      // 跳转到登录页
      Navigator.pushReplacementNamed(context, '/login');
    }
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("注册"),
        backgroundColor: Colors.green,
      ),
      body: SafeArea(
        minimum: const EdgeInsets.all(30),
        child: ListView(
          children: [
            TextField(
              controller: userNameController,
              decoration: const InputDecoration(labelText: '用户名', hintText: '请输入用户名'),
            ),
            const Padding(padding: EdgeInsets.all(10)),
            TextField(
              controller: passwordController,
              decoration: const InputDecoration(labelText: '密码', hintText: '请输入密码'),
            ),
            const Padding(padding: EdgeInsets.all(10)),
             TextField(
              controller: repeatPasswordController,
              decoration: const InputDecoration(labelText: '确认密码', hintText: '请输入确认密码'),
            ),
            const Padding(padding: EdgeInsets.all(10)),
            ElevatedButton(
              style: ElevatedButton.styleFrom(
                backgroundColor: Colors.green, // 设置按钮颜色
                foregroundColor: Colors.white, // 设置按钮文字颜色
              ),
              onPressed: ()=>_registerHandler(),
              child: const Text("注册")
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center, //设置主轴居中对齐
              children: [
                const Text("已有账号 "),
                TextButton(
                    child: const Text(
                      "去登录~",
                      style: TextStyle(
                        color: Colors.green, //设置字体颜色
                      ),
                    ),
                    onPressed: ()=>{
                      // 跳转到登录页面
                      Navigator.pushReplacementNamed(context, '/login')
                    },
                )
              ],
            )
          ],
        ),
      ),
    );
  }
}
